# from crypt import methods

from flask import Flask,request,jsonify,session
import os

app = Flask(__name__)
app.secret_key = os.urandom(24) # 设置一个秘钥，用户加密session数据


# 模拟用户信息，实际项目中需要替换为真实的用户信息

users = {
    "felix": "admin",
    "admin": "admin"

}

# 登录接口
# 条件，必须登录，才能获取用户信息
@app.route('/login', methods=['POST'])
def login():
    data = request.get_json()
    username = data.get("username")
    password = data.get("password")

    if username in users and users[username] == password:
        session["logged_in"] = True
        return 'login successful', 200
    else:
        return "login failed", 400

# 查询用户信息接口，需要登录才能访问
@app.route('/get_info')
def get_info():
    if "logged_in" in session and session['logged_in']:
        # 用户已登录，返回用户信息
        return "User Info Ok"
    else:
        # 用户未登录，返回未授权的状态码
        return "Unauthorized", 401

if __name__ == '__main__':
    app.run(debug=True)